package com.tangruojie.controller;

import com.tangruojie.dao.UserDao;
import com.tangruojie.domain.User;
import com.tangruojie.domain.query.UserQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserDao userDao;

    @PostMapping
    public String save(@RequestBody UserQuery userQuery) {
        User user = new User();
        BeanUtils.copyProperties(userQuery, user);
        return userDao.insert(user) > 0 ? "保存成功" : "保存失败";
    }

    @GetMapping
    public List<UserQuery> getAll() {
        return userDao.selectList(null)
                .stream()
                .map(user -> {
                    UserQuery userQuery = new UserQuery();
                    BeanUtils.copyProperties(user, userQuery);
                    return userQuery;
                }).toList();
    }

}
